﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;

namespace 包覆门.OperatorManager
{
    public partial class OperatorList : Form
    {
        public OperatorList()
        {
            InitializeComponent();
            dgUser.AutoGenerateColumns = false;
        }
        private UserDAL dal = new UserDAL();
        private DataTable dtUser = new DataTable();
        private Dictionary<int, string> dicauthority = new Dictionary<int, string>();//权限
        //加载数据
        private void OperatorManager_Load(object sender, EventArgs e)
        {
            dicauthority.Add(0, "接单员");
            dicauthority.Add(1, "管理员");
            dicauthority.Add(2, "统计员");
            BindingSource bs = new BindingSource();
            bs.DataSource = dicauthority;
            this.isadmin.DataSource = bs;
            this.isadmin.DisplayMember = "value";
            this.isadmin.ValueMember = "key";
            LoadData();
        }
        //加载数据
        private void LoadData() {
            string username = this.tbname.Text.Trim();
            string realname = this.tbrealname.Text.Trim();
            string phone = this.tbphone.Text.Trim();
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("username", username);
            dic.Add("realname",realname);
            dic.Add("phone",phone);

            this.dgUser.DataSource = dal.ListAll(dic);
        }
        //关闭之前清空om
        private void OperatorEditing_FormClosing(object sender, FormClosingEventArgs e)
        {
            mainform.om = null;
        }
        //新增
        private void tsbAdd_Click(object sender, EventArgs e)
        {
            OperatorEditing oe = new OperatorEditing();
            oe.IsNew = true;
            if (oe.ShowDialog() == DialogResult.OK) {
                LoadData();
            }
        }
        //编辑
        private void tsbEdit_Click(object sender, EventArgs e)
        {
            int count = dgUser.SelectedRows.Count;
            if (count <=0) {
                MessageBox.Show("请选中要编辑的行");
                return;
            }
            OperatorEditing oe = new OperatorEditing();
            oe.IsNew = false;
            oe.EditingId = dgUser.SelectedRows[0].Cells["uid"].Value.ToString();
            if (oe.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }
        //删除
        private void tsbDel_Click(object sender, EventArgs e)
        {
            int count = dgUser.SelectedRows.Count;
            if(count <=0){
                MessageBox.Show("请选中要删除的行");
                return ;
            }
            string uid = dgUser.SelectedRows[0].Cells["uid"].Value.ToString();
            String info = "您确定要删除账号：" + dgUser.SelectedRows[0].Cells["username"].Value.ToString() + "吗";
            if (DialogResult.OK == MessageBox.Show(info, "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)) {
                if (dal.DelById(uid))
                {
                    MessageBox.Show("删除成功");
                    LoadData();
                }
                else {
                    MessageBox.Show("删除失败");
                    LoadData();
                }
            }
        }
        //关闭
        private void tsbClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
